//定义关于counter的store
import {defineStore} from 'pinia'
import {Service} from "../requests/index.ts";
import {message} from "ant-design-vue";
import project from "./project.js";

/*defineStore 是需要传参数的，其中第一个参数是id，就是一个唯一的值，
简单点说就可以理解成是一个命名空间.
第二个参数就是一个对象，里面有三个模块需要处理，第一个是 state，
第二个是 getters，第三个是 actions。
*/
const useDemand = defineStore("demand",{
    state:() => ({
        demandList: [],//Demand列表
        currentDemand: {},//当前Demand名称
        planResult:{},//规划结果数组
        workRoute:{},//工作路由
        protectRoute:{},//保护路由
        survivalResult: {}, // 存储轮询结果
    }),

    getters: {
        //基本使用
        demandNameList(state) {
            return state.demandList.map((item) => {
                return item.name
            })
        },
        //获取业务所有路由
        workRouteList(state) {
            //如果没有进行业务规划，则返回空数组
            if (state.planResult[this.demandNameList[0]]==null){
                return [];
            }

            return state.demandList.map((item,id) => {
                // 直接从业务数据中获取路由信息
                return {
                    id:id,
                    name:item.name,
                    protect:item.protect,
                    result:state.planResult[item.name],
                    demandHead:item.demandHead,
                    demandTail:item.demandTail,
                    // 网络1的路由直接使用接口返回的数据
                    routeFibers: item.routeFibers || [],
                    protectRouteFibers: item.protectRouteFibers || [],
                    // 网络2的路由
                    doubleRouteFibers: item.doubleRouteFibers || [],
                    doubleProtectRouteFibers: item.doubleProtectRouteFibers || [],
                    // 保存源节点和目标节点信息
                    sourceNode: item.sourceNode,
                    targetNode: item.targetNode
                }
            })
        },


    }
    ,

    actions: {
        //基本使用
        selectCurrentDemand(name) {
            return this.currentDemand.name = name
        },
        //获取Demand列表
        async getDemandList(projectName) {
            const res = await Service.getDemandListUsingGet(projectName)
            console.log("getDemandList", res)
            if (res.respCode === 200) {
                console.log("getDemandList response data:", res.data)
                // 确保每个业务对象都有 protect 字段
                this.demandList = res.data.map(item => ({
                    ...item,
                    protect: item.protect || '无'  // 如果 protect 不存在，设置默认值
                }));
            } else {
                message.error(res.message);
            }
            return res.data
        },
        //添加单个Demand
        async addSingleDemand(projectName,demand) {
            console.log("demand",demand)
            const res=await Service.upLoadSingleDemandUsingPost(projectName,demand)
            // console.log("res",res)
            if (res.respCode === 200) {
                this.demandList= res.data;
                return res.data
            }else {
                message.error(res.message);
            }
            // console.log("addSingleDemand",this.demandList)
            return this.demandList
        },
        //文件上传
        async uploadDemandFile(file,projectName) {
            const res=await Service.upLoadDemandUsingPost(file,projectName)
            // console.log("res",res)
            if (res.respCode === 200) {
                this.demandList= res.data;
                message.success(file.name+"上传成功")
                return res.data
            }else {
                message.error(res.message);
            }
            // console.log("addSingleFiber",this.fiberList)
            return this.demandList
        },
        //删除Demand
        async delSingleDemand(projectName,name) {
            const res=await Service.deleteDemandUsingDelete(projectName,name)
            console.log("deleteDemandUsingDelete",res)
            if (res.respCode === 200) {
                this.demandList = res.data
                return res.data
            }else {
                message.error(res.message);
            }
            return this.demandList
        },

        //修改单个Demand
        async updateSingleDemand(projectName,demand) {
            const res=await Service.modifyDemandUsingPut(projectName,demand)
            console.log("updateSingleDemand",res)
            if (res.respCode === 200) {
                this.demandList = res.data
                return res.data
            }else {
                message.error(res.message);
            }
            return this.demandList
        },

        //规划业务
        async planDemand(projectName,layer,strategy,breakWeight,resourceWeight,lengthWeight) {
            const res=await Service.planUsingPost(projectName,layer,strategy,breakWeight,resourceWeight,lengthWeight)
            console.log("planDemand",res)
            if (res.respCode === 200) {
                this.planResult = res.data
                return res.data
            }else {
                message.error(res.message);
            }
             return this.planResult;
        },

        //获取所有业务工作路由
        async getWorkRoute(projectName) {
            const res=await Service.getRouteUsingGet(projectName)
            if (res.respCode === 200) {
                console.log("getWorkRoute",res)
                this.workRoute = res.data
            }else {
                message.error(res.message);
            }
            return res.data
        },

        //获取所有业务保护路由
        async getProtectRoute(projectName) {
            const res=await Service.getProtectRouteUsingGet(projectName)
            if (res.respCode === 200) {
                console.log("getProtectRoute",res)
                this.protectRoute = res.data
            }else {
                message.error(res.message);
            }
            return res.data
        },

        //选中当前业务
        selectDemand(demandObj) {
            this.currentDemand=demandObj;
        },

        //根据name获取业务详情信息
        getDemandDetailByName(name) {
            return this.demandList.find(item => item.name === name)
        },

        // 获取单个业务详情
        async getSingleDemand(projectName, demandName) {
            const res = await Service.getSingleDemandUsingGet(projectName, demandName)
            console.log("getSingleDemand", res)
            if (res.respCode === 200) {
                return res.data
            } else {
                message.error(res.message);
            }
            return null
        },

        // 存储轮询结果
        setSurvivalResult(result) {
            this.survivalResult = result;
        }
    }
})

//暴露这个demand模块
export default useDemand
